<?php

namespace App\Http\Controllers\Adm\Video;

use App\Libs\qCould\CosClient;
use App\Models\mcds_banner;
use App\Models\mcds_video;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class VideoController extends Controller
{
    /**
     * banner图列表
     */
    public function bannerlist()
    {
        $list = mcds_banner::getWhere(['is_valid'=>1]);
        return view('admin/video/bannerlist', ['list'=>$list]);
    }

    /**
     * 添加banner图
     * @param Request $request
     */
    public function addbanner(Request $request)
    {
        return view('admin/video/addbanner');
    }

    public function error()
    {
        return view('admin/error');
    }

    /**
     * 执行添加banner
     * @param Request $request
     */
    public function doaddbanner(Request $request)
    {
        $title = trim($request->title);
        $module = $request->input('module', 1);
        $img_url = $request->file('images');
        $img_url_two = $request->file('images_two');
        $sort = (int) $request->sort;
        if (empty($title) || empty($img_url) || empty($sort) || empty($img_url_two)) {
            return fun_error_view(0, '缺少参数', 'addbanner');
        }

        // 判断文件大小 文件格式
        $ext = strtolower($img_url->getClientOriginalExtension());     // 扩展名
        $ext2 = strtolower($img_url_two->getClientOriginalExtension());     // 扩展名
        if (!in_array($ext, ['png', 'jpg', 'jpeg']) || !in_array($ext2, ['png', 'jpg', 'jpeg'])) {
            return fun_error_view(0, '上传图片格式错误', 'addbanner');
        }
        if (isset($_FILES['images']['size']) && $_FILES['images']['size'] >= 8*1024*1024) {
            return fun_error_view(0, '上传图片大小不得超过8M', 'addbanner');
        }
        if (isset($_FILES['images_two']['size']) && $_FILES['images_two']['size'] >= 8*1024*1024) {
            return fun_error_view(0, '上传图片大小不得超过8M', 'addbanner');
        }

        // 轮播图
        $path = '/users/banner/' . date('Y/m/d/');
        $tem_img = 'banner_small_'.str_random(16) . '.png';
        $result = CosClient::upload($_FILES['images']['tmp_name'], $path.$tem_img);
        if (isset($result['code']) && $result['code'] != 0) {
            return fun_error_view(0, '上传图片失败', 'addbanner');
        }

        // 大图
        $path_two = '/users/banner/' . date('Y/m/d/');
        $tem_img_two  = 'banner_big_'.str_random(16) . '.png';
        $result_two = CosClient::upload($_FILES['images_two']['tmp_name'], $path_two.$tem_img_two);
        if (isset($result_two['code']) && $result_two['code'] != 0) {
            return fun_error_view(0, '上传图片失败', 'addbanner');
        }

        $data = [
            'title' => $title,
            'module' => $module,
            'img_url' => $path.$tem_img,
            'big_img_url' => $path_two.$tem_img_two,
            'sort' => $sort
        ];
        $res = mcds_banner::add($data);
        if (!$res) {
            return fun_error_view(0, '添加banner失败', 'addbanner');
        }
        return fun_error_view(0, '添加成功', 'bannerlist');

    }

    /**
     * 编辑banner页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function editBannerView(Request $request)
    {
        $id = (int) $request->id;
        if (empty($id)) {
            return fun_error_view(0, '缺少参数', 'bannerlist');
        }
        $info = mcds_banner::getOne(['id'=>$id]);
        if (empty($info)) {
            return fun_error_view(0, '数据不存在', 'bannerlist');
        }
        return view('admin/video/editbanner', ['info'=>$info]);
    }

    /**
     * 执行编辑banner操作
     * @param Request $request
     */
    public function doeditbanner(Request $request)
    {
        $id = (int) $request->id;
        $module = $request->input('module', 1);
        $title = trim($request->title);
        $img_url = $request->file('images');
        $img_url_two = $request->file('images_two');
        $sort = (int) $request->sort;
        $data = [];
        if (empty($title) || empty($sort) || empty($id)) {
            return fun_error_view(0, '数据不存在', 'editbanner?id='.$id);

        }
        // 判断文件大小 文件格式
        if ($img_url) {
            $ext = $img_url->getClientOriginalExtension();     // 扩展名
            if (!in_array($ext, ['png', 'jpg', 'jpeg'])) {
                return fun_error_view(0, '上传图片格式错误', 'editbanner?id='.$id);
            }
            if (isset($_FILES['images']['size']) && $_FILES['images']['size'] >= 8*1024*1024) {
                return fun_error_view(0, '上传图片大小不得超过8M', 'editbanner?id='.$id);
            }
        }

        if ($img_url_two) {
            $ext = $img_url_two->getClientOriginalExtension();     // 扩展名
            if (!in_array($ext, ['png', 'jpg', 'jpeg'])) {
                return fun_error_view(0, '上传图片格式错误', 'editbanner?id='.$id);
            }
            if (isset($_FILES['images_two']['size']) && $_FILES['images_two']['size'] >= 8*1024*1024) {
                return fun_error_view(0, '上传图片大小不得超过8M', 'editbanner?id='.$id);
            }
        }

        // 如果上传图片不为空修改图片
        if (!empty($img_url)) {
            $path = '/users/banner/' . date('Y/m/d/');
            $tem_img = 'banner_small_'.str_random(16) . '.png';
            $result = CosClient::upload($_FILES['images']['tmp_name'], $path.$tem_img);
            if (isset($result['code']) && $result['code'] != 0) {
                return fun_error_view(0, '上传图片失败', 'editbanner?id='.$id);
            }
            $data['img_url'] = $path.$tem_img;
        }
        if (!empty($img_url_two)) {
            $path = '/users/banner/' . date('Y/m/d/');
            $tem_img = 'banner_big_'.str_random(16) . '.png';
            $result = CosClient::upload($_FILES['images_two']['tmp_name'], $path.$tem_img);
            if (isset($result['code']) && $result['code'] != 0) {
                return fun_error_view(0, '上传图片失败', 'editbanner?id='.$id);
            }
            $data['big_img_url'] = $path.$tem_img;
        }

        $data['title'] = $title;
        $data['sort'] = $sort;
        $data['module'] = $module;
        $res = mcds_banner::edit(['id'=>$id], $data);
        if (!$res) {
            return fun_error_view(0, '修改banner失败', 'editbanner?id='.$id);
        }
        return fun_error_view(1, '修改banner成功', 'bannerlist');
    }

    /**
     * 删除banner
     * @param Request $request
     */
    public function delbanner(Request $request)
    {
        $id = (int) $request->id;
        if (empty($id)) {
            ajax_respon(0, '缺少参数');
        }
        $info = mcds_banner::edit(['id'=>$id], ['is_valid'=>0]);
        if (empty($info)) {
            ajax_respon(0, '删除失败');
        }
        ajax_respon(1, '删除banner成功');
    }

    /**
     * 视频列表
     */
    public function videolist()
    {
        $list = mcds_video::getWhere(['is_valid'=>1]);
        return view('admin/video/videolist', ['list'=>$list]);
    }

    /**
     * 添加视频
     */
    public function addvideo()
    {
        return view('admin/video/addvideo');
    }

    /**
     * 执行添加视频咨询
     */
    public function doaddvideo(Request $request)
    {
        $title = trim($request->title);
        $content = trim($request->input('contents', ''));
        $module = $request->input('module', 1);
        $img_url = $request->file('images');
        $video_url = $request->file('video_url');
        $sort = (int) $request->sort;
        if (empty($title) || empty($img_url) || empty($sort) || empty($video_url)) {
            return fun_error_view(0, '缺少参数', 'addvideo');
        }

        // 判断文件大小 文件格式
        $ext = strtolower($img_url->getClientOriginalExtension());     // 扩展名
        if (!in_array($ext, ['png', 'jpg', 'jpeg'])) {
            return fun_error_view(0, '上传图片格式错误', 'addvideo');
        }
        if (isset($_FILES['images']['size']) && $_FILES['images']['size'] > 8*1024*1024) {
            return fun_error_view(0, '上传图片大小不得超过8M', 'addvideo');
        }
        $video_ext = strtolower($video_url->getClientOriginalExtension());
        if (!in_array($video_ext, ['mp4', 'avi', 'rmvb'])) {
            return fun_error_view(0, '上传视频格式错误', 'addvideo');
        }
        if (isset($_FILES['video_url']['size']) && $_FILES['video_url']['size'] > 490*1024*1024) {
            return fun_error_view(0, '上传视频大小不得超过490M', 'addvideo');
        }

        // 上传图片
        $path = '/users/video/' . date('Y/m/d/');
        $tem_img = 'video_'.str_random(16) . '.png';
        $result = CosClient::upload($_FILES['images']['tmp_name'], $path.$tem_img);
        if (isset($result['code']) && $result['code'] != 0) {
            return fun_error_view(0, '上传图片失败', 'addvideo');
        }

        // 上传视频
        $video_path = '/users/video/' . date('Y/m/d/');
        $video_tem_img = 'video_'.str_random(16) . '.'.$video_ext;
        $video_result = CosClient::upload($_FILES['video_url']['tmp_name'], $video_path.$video_tem_img);
        if (isset($video_result['code']) && $video_result['code'] != 0) {
            return fun_error_view(0, '上传视频失败', 'addvideo');
        }

        /*$video_path = '/users/video/' . date('Y/m/d/');
        $video_tem_img = 'video_'.str_random(16) . '.'.$video_ext;
        $put_result = Storage::disk('local')->put(
            $video_tem_img,
            file_get_contents($video_url->getRealPath()),
            'public'
        );
        $video_result = CosClient::upload(storage_path('app/images/') . $video_tem_img, $video_path.$video_tem_img);
        if (isset($video_result['code']) && $video_result['code'] != 0) {
            return fun_error_view(0, '上传视频失败', 'editvideo?id='.$id);
        }
        unlink(storage_path('app/images/').$video_tem_img);*/

        $data = [
            'title' => $title,
            'module' => $module,
            'content' => $content,
            'img_url' => $path.$tem_img,
            'video_url' => $video_path.$video_tem_img,
            'sort' => $sort
        ];
        $res = mcds_video::add($data);
        if (!$res) {
            return fun_error_view(0, '添加视频咨询失败', 'addvideo');
        }
        return fun_error_view(1, '添加视频咨询成功', 'videolist');
    }

    /**
     * 编辑视频咨询页面
     * @param Request $request
     */
    public function editVideoView(Request $request)
    {
        $id = (int) $request->id;
        if (empty($id)) {
            return fun_error_view(0, '缺少参数', 'videolist');
        }
        $info = mcds_video::getOne(['id'=>$id]);
        if (empty($info)) {
            return fun_error_view(0, '数据不存在', 'videolist');
        }
        return view('admin/video/editvideo', ['info'=>$info]);
    }

    /**
     * 编辑视频咨询
     */
    public function doeditvideo(Request $request)
    {
        $id = (int) $request->id;
        $title = trim($request->title);
        $module = $request->input('module', 1);
        $content = trim($request->input('contents', ''));
        $img_url = $request->file('images');
        $video_url = $request->file('video_url');
        $sort = (int) $request->sort;
        if (empty($title) || empty($sort) || empty($id)) {
            return fun_error_view(0, '缺少参数', 'editvideo?id='.$id);
        }

        // 判断文件大小 文件格式
        if ($img_url) {
            $ext = strtolower($img_url->getClientOriginalExtension());     // 扩展名
            if (!in_array($ext, ['png', 'jpg', 'jpeg'])) {
                return fun_error_view(0, '上传图片格式错误', 'editvideo?id='.$id);
            }
            if (isset($_FILES['images']['size']) && $_FILES['images']['size'] > 8*1024*1024) {
                return fun_error_view(0, '上传图片大小不得超过8M', 'editvideo?id='.$id);
            }
        }

        if ($video_url) {
            $video_ext = strtolower($video_url->getClientOriginalExtension());
            if (!in_array($video_ext, ['mp4', 'avi', 'rmvb'])) {
                return fun_error_view(0, '上传视频格式错误', 'editvideo?id='.$id);
            }
            if (isset($_FILES['video_url']['size']) && $_FILES['video_url']['size'] > 490*1024*1024) {
                return fun_error_view(0, '上传视频大小不得超过490M', 'editvideo?id='.$id);
            }
        }


        // 上传图片
        $img_addres = '';
        if ($img_url) {
            $path = '/users/video/' . date('Y/m/d/');
            $tem_img = 'video_'.str_random(16) . '.png';
            $result = CosClient::upload($_FILES['images']['tmp_name'], $path.$tem_img);
            if (isset($result['code']) && $result['code'] != 0) {
                return fun_error_view(0, '上传图片失败', 'editvideo?id='.$id);
            }
            $img_addres = $path.$tem_img;
        }

        // 上传视频
        $video_addres = '';
        if ($video_url) {
            $video_path = '/users/video/' . date('Y/m/d/');
            $video_tem_img = 'video_'.str_random(16) . '.'.$video_ext;
            /*$put_result = Storage::disk('local')->put(
                $video_tem_img,
                file_get_contents($video_url->getRealPath()),
                'public'
            );
            $video_result = CosClient::upload(storage_path('app/images/') . $video_tem_img, $video_path.$video_tem_img);*/
            $video_result = CosClient::upload($_FILES['video_url']['tmp_name'], $video_path.$video_tem_img);
            if (isset($video_result['code']) && $video_result['code'] != 0) {
                return fun_error_view(0, '上传视频失败', 'editvideo?id='.$id);
            }
            $video_addres = $video_path.$video_tem_img;
            //unlink(storage_path('app/images/').$video_tem_img);
        }

        $data = [
            'title' => $title,
            'content' => $content,
            'sort' => $sort,
            'module' => $module
        ];
        if ($img_addres) {
            $data['img_url'] = $img_addres;
        }
        if ($video_addres) {
            $data['video_url'] = $video_addres;
        }

        $res = mcds_video::edit(['id'=>$id], $data);
        if (!$res) {
            return fun_error_view(0, '修改视频咨询失败', 'editvideo?id='.$id);
        }
        return fun_error_view(1, '修改视频咨询成功', 'videolist');
    }

    /**
     * 删除视频咨询
     * @param Request $request
     */
    public function delvideo(Request $request)
    {
        $id = (int) $request->id;
        if (empty($id)) {
            ajax_respon(0, '缺少参数');
        }
        $info = mcds_video::edit(['id'=>$id], ['is_valid'=>0]);
        if (empty($info)) {
            ajax_respon(0, '删除失败');
        }
        ajax_respon(1, '删除成功');
    }
}
